home *** CD-ROM | disk | FTP | other *** search
- // Copyright (C) 1997-2002 Alias|Wavefront,
- // a division of Silicon Graphics Limited.
- //
- // The information in this file is provided for the exclusive use of the
- // licensees of Alias|Wavefront. Such users have the right to use, modify,
- // and incorporate this code into other products for purposes authorized
- // by the Alias|Wavefront license agreement, without fee.
- //
- // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
- // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
- // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
- // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
- // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
- // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- //
- //
- // Description : make a regular polygon given the number of sides
- // and the length of a side.
- //
- // Usage :
- // 1. For a equilateral triangle of length 3 : makeRegularPolyon(3,2);
- // 2. For a hexagon of length 2.5 : makeRegularPolyon(6,2.5);
- //
- // Note : number of sides should be atleast 3. We could build
- // regular triangles, squares, pentagon, hexagon, ...., circle in the
- // limit. ? The polygon is created in the XY ( Z=0) plane.
-
- //
- // Find the radius of circle encompasing the polygon.
- //
- proc float boundingCircleRadius( int $n, float $x )
- //
- // Description:
- // number of sides on the polygon.
- // $x = length of the polygon.
- //
- {
- float $rad ;
- float $angle = 360.0 / $n ;
-
- float $cosx = cos(deg_to_rad($angle)) ;
- float $den = 2.0 * ( 1.0 - $cosx ) ;
- $rad = $x / sqrt($den) ;
- return $rad ;
- }
-
- proc float[] createRegularPolygonY( int $n, float $r )
- {
- int $i ;
- float $x[] ;
- float $angle = 360.0 / $n ;
-
- for( $i = 0 ; $i < $n ; $i++ ) {
- float $a = $i * $angle ;
- $x[$i] = $r * cos(deg_to_rad($a)) ;
- }
- return $x ;
- }
-
- proc float[] createRegularPolygonX( int $n,float $r )
- {
- int $i ;
- float $x[] ;
- float $angle = 360.0 / $n ;
- for( $i = 0 ; $i < $n ; $i++ ) {
- float $a = $i * $angle ;
- $x[$i] = $r * sin(deg_to_rad($a)) ;
- }
- return $x ;
- }
-
-
- global proc int makeRegularPolygon( int $n, float $len )
-
- {
-
- if( $n <= 2 ) {
- error "A regular polygon must have atleast 3 sides" ;
- return 1 ;
- }
-
- float $r = boundingCircleRadius( $n, $len ) ;
-
- float $x[] = createRegularPolygonX( $n, $r ) ;
- float $y[] = createRegularPolygonY( $n, $r ) ;
-
- // create the n-sided polygon.
- //
- string $facet[] ;
- string $cmd = "polyCreateFacet -ch 0 ";
- string $vertStr ;
- float $z = 0.0 ;
- int $i ;
- for( $i = 0 ; $i < $n ; $i++ ) {
- $vertStr += " -p " ;
- $vertStr += $x[$i] ;
- $vertStr += " " ;
- $vertStr += $y[$i] ;
- $vertStr += " " ;
- $vertStr += $z ;
- }
- $cmd += $vertStr ;
- $cmd += " ";
-
- // do the polyCreateFacet cmd.
- //
- string $facet[] = eval($cmd) ;
- select -r $facet[0] ;
- return 0 ;
-
- }
-
-